package com.dgut.forum.services.impl;

import com.dgut.forum.common.AppResult;
import com.dgut.forum.common.MessageState;
import com.dgut.forum.common.ResultCode;
import com.dgut.forum.dao.MessageMapper;
import com.dgut.forum.exception.ApplicationException;
import com.dgut.forum.model.Message;
import com.dgut.forum.model.User;
import com.dgut.forum.services.IMessageService;
import com.dgut.forum.utils.StringUtil;
import com.mysql.jdbc.Messages;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class MessageServiceImpl implements IMessageService {

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private MessageMapper messageMapper;
    @Override
    public void createMessage(Message message) {
        if(message.getPostUserId()==null||message.getReceiveUserId()==null||StringUtil.isEmpty(message.getContent())){
            log.warn("发生私信时参数校验失败");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        User user1=userService.selectByPrimaryKey(message.getReceiveUserId());
        if(user1==null||user1.getDeleteState()==1){
            log.warn("私信接收者不存在");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        User user2=userService.selectByPrimaryKey(message.getPostUserId());
        if(user2==null||user2.getDeleteState()==1){
            log.warn("私信发送者不存在");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        if(message.getReceiveUserId()==message.getPostUserId()){
            log.info("不能给自己发送私信");
            throw new ApplicationException(AppResult.failed("不能给自己发送私信"));
        }
        int row=messageMapper.insertSelective(message);
        if(row!=1){
            log.error("发私信,发生服务器错误");
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        log.info("发送私信成功");
    }

    @Override
    public int selectMessageUnRead(Long receiveUserId) {
       if(receiveUserId==null||receiveUserId<=0){
           log.warn("查看未读信息个数是,参数校验失败");
           throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
       }
       User user=userService.selectByPrimaryKey(receiveUserId);
       if(user==null||user.getDeleteState()==1){
            log.warn("私信接收者不存在");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
       }
        int count=messageMapper.selectMessageCountUnRead(receiveUserId);
        log.info("查询未读消息条数成功,count="+count);
        return count;
    }

    @Override
    public Message selectById(Long id) {
        if(id==null||id<=0){
            log.warn("查看私信,参数校验失败");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return messageMapper.selectByPrimaryKey(id);

    }

    @Override
    public List<Message> getAll(Long receiveUserId) {
        if(receiveUserId==null||receiveUserId<=0){
            log.warn("查看私信,参数校验失败");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return messageMapper.selectAll(receiveUserId);
    }

    @Override
    public void updateState(Long id,byte type) {
        if(id<=0||type<MessageState.UNREAD.getType()||type>MessageState.REPLIED.getType()){
            log.warn("修改私信状态,参数校验失败");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message message=messageMapper.selectByPrimaryKey(id);
        if(message==null||message.getDeleteState()==1){
            log.warn("私信被删除,id="+id);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }
        Message updateMessage=new Message();
        updateMessage.setId(id);
        updateMessage.setState(type);
        int row=messageMapper.updateByPrimaryKeySelective(updateMessage);
        if(row!=1){
            log.error("修改私信状态,发生服务器错误");
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        log.info("修改私信状态成功");
    }

    @Override
    public void reply(Long id, Message message) {
        if(id==null||id<=0){
            log.warn("回复私信,参数校验失败");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message existsMessage=selectById(id);
        if(existsMessage==null) {
            log.error("私信不存在,发生服务器错误");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }

        //更新私信为已读
        updateState(id,MessageState.REPLIED.getType());
        //插入回复私信
        createMessage(message);

        log.info("回复私信成功");
    }
}
